﻿using EquipmentAlarmSystem.Service.Library;
using EquipmentAlarmSystem.Service.Models;
using EquipmentAlarmSystem.Service.Models.Enum;

namespace EquipmentAlarmSystem.Service.ContentPushUnit;

public static class SendMessage
{
    /// <summary>
    /// 日志记录
    /// </summary>
    static Logging logging = new("SendMessage");

    /// <summary>
    /// 消息接收服务
    /// </summary>
    /// <param name="contacts">联系人</param>
    /// <param name="name">设备名称</param>
    /// <param name="information">信息</param>
    /// <param name="level">等级</param>
    public static void Receive(DeviceInformation device, string information, UserLevel level = UserLevel.User)
    {
        //获取模板
        string time = Basics.GetDate;
        string template = $"设备报警通知;时间：{time};编号：{device.Id};设备：{device.Name};信息：{information}";

        logging.Info(template);

        //确认推送服务开启
        if (Basics.Config.EnablePush)
        {
            SendMSM(device.Contacts, template.Replace(";", "\n"), level);
            //拨号内容置空，挂板
            SensCall(device.Contacts, template.Replace($"时间：{time};编号：{device.Id};", ""), level);
        }
    }

    /// <summary>
    /// 短信发送
    /// </summary>
    /// <param name="contacts"></param>
    /// <param name="content"></param>
    /// <param name="level"></param>
    private static void SendMSM(List<Models.Contact> contacts, string content, Models.Enum.UserLevel level)
    {
        List<string> contactsList = new();
        if (contacts != null && contacts.Count >= 1 && IsItDuringTheAlarmPeriod())
        {
            foreach (var itme in contacts)
            {
                if (itme.EnableSms)
                {
                    foreach (var itemc in Basics.Config.Contacts)
                    {
                        if (itme.Name == itemc.Name)
                        {
                            contactsList.Add(itemc.PhoneNumber);
                        }
                    }
                }
            }
        }

        foreach (var item in Basics.Config.Contacts)
        {
            //工程师
            if (level == UserLevel.Engineer)
            {
                if (item.Level == Models.Enum.UserLevel.Engineer)
                {
                    if (item.EnableSms)
                    {
                        contactsList.Add(item.PhoneNumber);
                    }
                }
            }
            //管理员
            if (level == Models.Enum.UserLevel.Administrators)
            {
                if (item.Level == Models.Enum.UserLevel.Administrators)
                {
                    if (item.EnableSms)
                    {
                        contactsList.Add(item.PhoneNumber);
                    }
                }
            }
            //全局联系人
            if (item.Level == Models.Enum.UserLevel.GlobalUser)
            {
                if (item.EnableSms)
                {
                    contactsList.Add(item.PhoneNumber);
                }
            }
        }
        InformationPushService.Add(MessageType.Sms, contactsList, content);
    }

    /// <summary>
    /// 拨打电话
    /// </summary>
    /// <param name="contacts"></param>
    /// <param name="content"></param>
    /// <param name="level"></param>
    private static void SensCall(List<Models.Contact> contacts, string content, UserLevel level)
    {
        //SystemConfiguration.Configuration.configuration.Contacts
        List<string> contactsList = new();
        //联系人合并
        if (contacts != null && contacts.Count >= 1 && IsItDuringTheAlarmPeriod())
        {
            foreach (var itme in contacts)
            {
                if (itme.EnableDialing)
                {
                    foreach (var itemc in Basics.Config.Contacts)
                    {
                        if (itme.Name == itemc.Name)
                        {
                            contactsList.Add(itemc.PhoneNumber);
                        }
                    }
                }
            }
        }

        foreach (var item in Basics.Config.Contacts)
        {
            //工程师
            if (level == UserLevel.Engineer)
            {
                if (item.Level == UserLevel.Engineer)
                {
                    if (item.EnableDialing)
                    {
                        contactsList.Add(item.PhoneNumber);
                    }
                }
            }
            //管理员
            if (level == UserLevel.Administrators)
            {
                if (item.Level == UserLevel.Administrators)
                {
                    if (item.EnableDialing)
                    {
                        contactsList.Add(item.PhoneNumber);
                    }
                }
            }
            //全局联系人
            if (item.Level == UserLevel.GlobalUser)
            {
                if (item.EnableDialing)
                {
                    contactsList.Add(item.PhoneNumber);
                }
            }
        }
        InformationPushService.Add(MessageType.Call, contactsList, content);
    }

    /// <summary>
    /// 是否在报警时段
    /// </summary>
    /// <returns></returns>
    private static bool IsItDuringTheAlarmPeriod()
    {
        if (Basics.Config.EnableAlarmPush)
        {
            // 6 <= 1
            if (Basics.Config.EnableAlarmPushTime <= DateTime.Now.Hour == false)
            {
                return false;
            }
            // 18 >= 1
            if (Basics.Config.DisableAlarmPushTime >= DateTime.Now.Hour == false)
            {
                return false;
            }
        }
        return true;
    }
}
